<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page2 extends Admin_Controller
{
	/**
	* called in module
	*/
	function dropdown()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_dropdown_model');
		
		// add to view
		$this->add('name', $CI->page2_dropdown_model->name);
		$this->add('onchange', $CI->page2_dropdown_model->onchange);
		$this->add('value', $CI->page2_dropdown_model->value);
		$this->add('items', $CI->page2_dropdown_model->items);
		
		// view
		$this->view('page2_dropdown_view');
	}
	
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_list_model');
		
		// filter
		if (isset($_POST['filter']))
			$CI->page2_list_model->cache->add($CI->page2_list_model->filter_cache_prefix, $_POST['filter']);

		// sql
		$CI->db->where(PAGE.'.'.PAGE_FRANCHISE_ID, $CI->page2_list_model->admin_id());
		$q = $CI->db->get(PAGE);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->page2_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->page2_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->from(PAGE);
		$CI->db->select('*');
		$CI->db->where(PAGE.'.'.PAGE_FRANCHISE_ID, $CI->page2_list_model->admin_id());
		foreach ($CI->page2_list_model->columns as $k=>$v)
		{
			if ($CI->page2_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->page2_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->page2_list_model->start);
		$q = $CI->db->get();
		
		// gridview
		$this->add('items', $q->result());
		$this->add('start', $CI->page2_list_model->start);
		foreach ($CI->page2_list_model->columns as $k=>$v)
		{
			if ($CI->page2_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->page2_list_model->cache->load($k).'"');
			else
				$this->add('sort_'.$k,  '');
		}

		// message
		$this->add('msg', $CI->page2_list_model->cache->load($CI->page2_list_model->msg_cache_prefix));
		$CI->page2_list_model->cache->add($CI->page2_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('page2.toolbar', array('parent_id'=>0, 'start'=>$CI->page2_list_model->start));
		$this->module_navigation('Page Management');
		
		// view
		$this->view('page2_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2.page2_detail_model');
		
		// sql	
		$q = $CI->db->get_where(PAGE, array(PAGE_FRANCHISE_ID=>$CI->page2_detail_model->admin_id(), PAGE_PAGE_ID=>$CI->page2_detail_model->item));
		$entity = $q->row();
		
		// error
		$post = $CI->page2_detail_model->cache->load('post');
		$CI->page2_detail_model->cache->add('post', '');

		// add to view
		$this->add('title', $post?$post['title']:($entity?$entity->{PAGE_TITLE}:''));
		
		// bg dropdown
		$CI->load->module_model('page2_css.page2_css_dropdown_model');
		$CI->page2_css_dropdown_model->name = 'css_file';
		$CI->page2_css_dropdown_model->value = $post?$post['css_file']:($entity?$entity->{PAGE_CSS_FILE}:'');
		$CI->page2_css_dropdown_model->addOption(array(PAGE_CSS_CSS_FILE=>'', PAGE_CSS_CSS_FILE=>'----- Select Colour -----'));
		
		// block dropdowns
		$CI->load->module_model('page2.page2_model');
		$CI->load->module_model('block.block_dropdown_model');
		$CI->block_dropdown_model->addOption(array(BLOCK_BLOCK_ID=>0, BLOCK_NAME=>'----- Select Block -----'));
		
		for($j=1; $j<=3; $j++)
		{	
			// limit in columns
			$limit = $CI->page2_detail_model->cache->load('limit'.$j);	
			if (!$limit) $limit = 5;
			$limit2 = $CI->page2_model->limit_blocks_in_column($CI->page2_detail_model->item, $j);
			if ($limit2 >= $limit) 
			{
				$limit = $limit2;
				$this->add('limit'.$j, 0);
			} else if ($limit < 2)
				$this->add('limit'.$j, 0);
			else
				$this->add('limit'.$j, 1);
			$CI->page2_detail_model->cache->add('limit'.$j, $limit);
			
			// add to view
			$row = array();
			$selections = $CI->page2_model->blocks_in_column($CI->page2_detail_model->item, $j);
			for($i=1; $i<=$limit; $i++)
			{
				$name = 'col_'.$j.'_'.$i;
				$CI->block_dropdown_model->name = $name;
				$CI->block_dropdown_model->value = 0;

				$bg = '';
				if (!$post)
				{
					foreach ($selections as $selection)
					{
						if ($i == $selection->{CELL_LINK_ORDER_ID})
						{
							$bg = ' bgcolor="#FF9900"';
							$CI->block_dropdown_model->value = $selection->{CELL_LINK_BLOCK_ID};
							break;
						}
					}
				} else {
					if (isset($post[$name]))
					{						
						if ($post[$name])
							$bg = ' bgcolor="#C3C3C3"';
						foreach ($selections as $selection)
						{
							if ($i == $selection->{CELL_LINK_ORDER_ID})
							{
								if ($post[$name] && $selection->{CELL_LINK_BLOCK_ID} == $post[$name])
								{
									$bg = ' bgcolor="#FF9900"';
								} else
									$bg = ' bgcolor="#C3C3C3"';
								break;
							}
						}
						$CI->block_dropdown_model->value = $post[$name];
					}
				}
				
				// add to view
				$dd = Modules::run('block.block.dropdown', TRUE);
				$row[] = array('bg'=>$bg, 'block_dropdown'=>$dd);
			}
			$this->add('row'.$j, $row);
		}

		// add to view
		$this->add('start', $CI->page2_detail_model->start);
		$this->add('item_id', $CI->page2_detail_model->item);

		// message
		$this->add('msg', $CI->page2_detail_model->cache->load($CI->page2_detail_model->msg_cache_prefix));
		$CI->page2_detail_model->cache->add($CI->page2_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->page2_detail_model->item)
			$this->module_navigation('Page : Edit');
		else
			$this->module_navigation('Page : New');
			
		// view
		$this->view('page2_detail_view');
	}
}